// Copyright DustedPixels.com 2008. All rights reserved.

package com.dustedpixels.jasmin.unit.video;

import com.dustedpixels.jasmin.unit.Unit;

/**
 * Helper unit, which may be used to count rows and cols.
 * 
 * @author micapolos@gmail.com (Michal Pociecha-Los)
 */
public final class RowColCounter implements Unit {
  // (input) clock source
  public boolean CLOCK;
  
  // (input) row counter
  public int ROW;
  
  // (output) column counter
  public int COL;

  private boolean lastClock = false;
    
  private final int rows;
  private final int cols;
  
  public RowColCounter(int rows, int cols, int initialRow, int initialCol) {
    this.rows = rows;
    this.cols = cols;
    this.ROW = initialRow;
    this.COL = initialCol;
  }
  
  public void updateEndPoints() {
    if (lastClock != CLOCK) {
      if (CLOCK) {
        // Update counters
        if (++COL == cols) {
          COL = 0;
          if (++ROW == rows) {
            ROW = 0;
          }
        }        
      }
    
      lastClock = CLOCK;
    }
  }
}
